/*	
*	File: ipv4-packet.h
*	Date: 2011/11/06
*	User: Ronmaru
*	Desctiption: ipv4 packet class
*/

#ifndef _IPV4_PACKET_H_
#define _IPV4_PACKET_H_

#include "pcraft-tool.h"
#include <string>
#include <istream>
#include <iostream>
#include <ostream>
#include <boost/bind.hpp>
#include <boost/asio/streambuf.hpp>

class IPV4packet
{
public:
  IPV4packet(boost::asio::io_service& io_service, const IPV4header &header, const std::string &body) 
    : ipv4_header_(header), ipv4_body_(body), ipv4_socket_(io_service, raw::v4())
  {
    raw::resolver resolver(io_service);
    raw::resolver::query query(raw::v4(), header.GetDestinationAddress().to_string(), "");
    raw::endpoint destination = *resolver.resolve(query);
    ipv4_socket_.connect(destination);

    const ip_hdrincl opt_ip_hdrincl(1);
    ipv4_socket_.set_option(opt_ip_hdrincl);
  }

  void send()
  {
    boost::asio::streambuf request_buffer;
    std::ostream os(&request_buffer);
    os << ipv4_header_ << ipv4_body_;

    ipv4_socket_.send(request_buffer.data());
  }

private:
  raw::socket ipv4_socket_;
  IPV4header ipv4_header_;
  std::string ipv4_body_;
};

#endif //_IPV4_PACKET_H_